package com.bulletelc.apollopre.module.display.product.details.introduce;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.ProductAssociate;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.module.display.product.details.ProductDetailsActivity;

import java.util.List;

/**
 * Created by Administrator on 2018/5/3.
 */

public class AssociateAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private List<ProductAssociate> productAssociates;
    private Context context;
    LayoutInflater inflater;
    private OnItemCheckChangeListener listener;

    public AssociateAdapter(Context context, List<ProductAssociate> productAssociates) {
        this.productAssociates = productAssociates;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return productAssociates != null ? productAssociates.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return productAssociates.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ProductAssociate productAssociate = productAssociates.get(i);
        ViewHolder viewHolder;
        view = inflater.inflate(R.layout.product_associate_list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.llItem = view.findViewById(R.id.llItem);
        viewHolder.ivPic = view.findViewById(R.id.ivPic);
        viewHolder.tvName = view.findViewById(R.id.tvName);
        viewHolder.tvPrice = view.findViewById(R.id.tvPrice);
        viewHolder.cbAssociate = view.findViewById(R.id.cbAssociate);
        view.setTag(viewHolder);

        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(viewHolder.ivPic, productAssociate.getImage_url()));
        viewHolder.tvName.setText(productAssociate.getName());
        viewHolder.tvPrice.setText("¥" + productAssociate.getSales_price());
        viewHolder.cbAssociate.setChecked(productAssociate.getChosen());
        viewHolder.cbAssociate.setOnCheckedChangeListener(this);
        viewHolder.llItem.setOnClickListener(this);
        viewHolder.llItem.setTag(i);
        viewHolder.cbAssociate.setTag(i);
        return view;
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        int pos = (int) compoundButton.getTag();
        double price = Double.parseDouble(productAssociates.get(pos).getSales_price());
        if (b) {
            if (listener != null) {
                listener.checked(price);
            }
        } else {
            listener.checked(-price);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llItem:
                int pos = (int) view.getTag();
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_PRODUCT_ID, productAssociates.get(pos).getId());
                context.startActivity(intent);
                break;
        }
    }

    class ViewHolder {
        private LinearLayout llItem;
        private ImageView ivPic;
        private TextView tvName;
        private TextView tvPrice;
        private CheckBox cbAssociate;
    }

    public void setOnItemCheckChangeListener(OnItemCheckChangeListener listener) {
        this.listener = listener;
    }

    interface OnItemCheckChangeListener {
        void checked(double price);
    }

}
